package Medium.Recursive;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: white-zhou
 * @date: 2024-09-23 18:14
 **/
public class Permute {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        boolean[] used = new boolean[nums.length];
        dfs(nums, 0, path, used, res);
        return res;
    }

    public void dfs(int[] nums, int depth, List<Integer> path, boolean[] used, List<List<Integer>> res) {
        if (depth == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!used[i]) {
                path.add(nums[i]);
                used[i] = true;
                dfs(nums, depth + 1, path, used, res);
                path.remove(path.size() - 1);
                used[i] = false;
            }
        }
    }
}
